package cn.aran.sys.controller;

import cn.aran.basic.annotation.PreAuthorize;
import cn.aran.basic.utils.AjaxResult;
import cn.aran.basic.utils.PageList;
import cn.aran.sys.domain.DictionaryDetail;
import cn.aran.sys.query.DictionaryDetailQuery;
import cn.aran.sys.service.IDictionaryDetailService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Api(value = "数据字典明细的API", description = "数据字典明细相关的CRUD功能")
@RestController
@RequestMapping("/dictionaryDetail")
public class DictionaryDetailController {
    @Autowired
    private IDictionaryDetailService dictionaryDetailService;

    /**
     * @Title: loadById
     * @Description: 根据ID查询数据字典明细
     * @Author: Aran
     * @Version: 1.0
     * @Date:  2022/6/26 14:55
     * @Parameters: [id]
     * @Return cn.aran.sys.domain.DictionaryDetail
     */
    @ApiOperation(value = "通过ID查询")
    @GetMapping("/{id}")
    public DictionaryDetail loadById(@ApiParam(value = "主键ID属性", required = true) @PathVariable("id") Long id) {
        return dictionaryDetailService.loadById(id);
    }

    /**
     * @Title: loadAll
     * @Description: 查询数据字典明细所有
     * @Author: Aran
     * @Version: 1.0
     * @Date:  2022/6/26 14:55
     * @Parameters: []
     * @Return java.util.List<cn.aran.sys.domain.DictionaryDetail>
     */
    @ApiOperation(value = "查询全部")
    @GetMapping
    public List<DictionaryDetail> loadAll() {
        List<DictionaryDetail> dictionaryDetails = dictionaryDetailService.loadAll();
        return dictionaryDetails;
    }

    /**
     * @Title: remove
     * @Description: 根据id删除数据字典明细
     * @Author: Aran
     * @Version: 1.0
     * @Date:  2022/6/26 14:56
     * @Parameters: [id]
     * @Return cn.aran.basic.util.AjaxResult
     */
    @ApiOperation(value = "数据字典明细删除")
    @DeleteMapping("/{id}")
    @PreAuthorize(name = "删除数据字典明细", sn = "dictionaryDetail:delete")
    public AjaxResult remove(@ApiParam(value = "主键ID属性", required = true) @PathVariable("id") Long id) {
        try {
            dictionaryDetailService.remove(id);
            return AjaxResult.me();
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMessage("系统繁忙,请稍后重试!");
        }

    }

    /**
     * @Title: batchRemove
     * @Description: 数据字典明细批量删除
     * @Author: Aran
     * @Version: 1.0
     * @Date:  2022/6/26 14:56
     * @Parameters: [ids]
     * @Return cn.aran.basic.util.AjaxResult
     */
    @PatchMapping
    @PreAuthorize(name = "批量删除数据字典明细", sn = "dictionaryDetail:patch")
    public AjaxResult batchRemove(@RequestBody List<Long> ids) {
        try {
            dictionaryDetailService.batchRemove(ids);
            return AjaxResult.me();
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMessage("系统繁忙,请稍后重试!");
        }

    }

    /**
     * @Title: batchRemove
     * @Description: 通过数据字典类型删除明细
     * @Author: Aran
     * @Version: 1.0
     * @Date:  2022/6/26 14:56
     * @Parameters: [ids]
     * @Return cn.aran.basic.util.AjaxResult
     */
    @PatchMapping("/{id}")
    public AjaxResult batchDelete(@ApiParam(value = "主键ID属性", required = true) @PathVariable("id") Long id) {
        try {
            dictionaryDetailService.delete(id);
            return AjaxResult.me();
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMessage("系统繁忙,请稍后重试!");
        }

    }

    /**
     * @Title: saveOrUpt
     * @Description: 新增修改数据字典明细
     * @Author: Aran
     * @Version: 1.0
     * @Date:  2022/6/26 14:56
     * @Parameters: [dictionaryDetail]
     * @Return cn.aran.basic.util.AjaxResult
     */
    @ApiOperation(value = "数据字典明细新增或修改")
    @PutMapping
    @PreAuthorize(name = "保存数据字典明细", sn = "dictionaryDetail:save")
    public AjaxResult saveOrUpt(@RequestBody DictionaryDetail dictionaryDetail) {
        try {
            if (dictionaryDetail.getId() == null) {
                dictionaryDetailService.save(dictionaryDetail);
            } else {
                dictionaryDetailService.update(dictionaryDetail);
            }
            return AjaxResult.me();
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMessage("系统繁忙,请稍后重试!");
        }
    }

    /**
     * @Title: queryData
     * @Description: 数据字典明细高级查询,分页
     * @Author: Aran
     * @Version: 1.0
     * @Date:  2022/6/26 14:56
     * @Parameters: [dictionaryDetailQuery]
     * @Return cn.aran.basic.util.PageList<cn.aran.sys.domain.DictionaryDetail>
     */
    @ApiOperation(value = "数据字典明细高级查询和分页")
    @PostMapping
    @PreAuthorize(name = "列表数据字典明细", sn = "dictionaryDetail:list")
    public PageList<DictionaryDetail> queryData(@RequestBody DictionaryDetailQuery dictionaryDetailQuery) {
        PageList<DictionaryDetail> data = dictionaryDetailService.queryData(dictionaryDetailQuery);
        return data;
    }

}
